package com.zide.framework.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zide.common.constant.HttpStatus;
import com.zide.common.core.domain.entity.SysUser;
import com.zide.common.core.domain.entity.UmsMember;
import com.zide.common.core.domain.model.LoginUser;
import com.zide.common.core.domain.model.MemberLoginUser;
import com.zide.common.enums.UserStatus;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.MessageUtils;
import com.zide.common.utils.SecurityUtils;
import com.zide.common.utils.StringUtils;
import com.zide.framework.security.service.SysPasswordService;
import com.zide.framework.web.domain.vo.save.MemberRegisterVO;
import com.zide.framework.web.mapper.UmsMemberMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 用户验证处理
 *
 * @author xianying
 */
@Service
@Slf4j
public class MemberUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UmsMemberMapper memberMapper;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        LambdaQueryWrapper<UmsMember> wrapper = new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getMobile, account).or().eq(UmsMember::getAccount, account);
        UmsMember dbBean = memberMapper.selectOne(wrapper);
        log.info("loadUserByUsername=====>{}",dbBean);
        if (StringUtils.isNull(dbBean)) {
            log.info("登录用户：{} 不存在.", account);
            throw new ServiceException(MessageUtils.message("user.not.exists"));
        }  else if (UserStatus.DISABLE.getCode().equals(dbBean.getStatus())) {
            log.info("登录用户：{} 已被停用.", account);
            throw new ServiceException(MessageUtils.message("user.blocked"));
        }
        return BeanUtil.copyProperties(dbBean, MemberLoginUser.class);
    }

    private boolean checkRegisterByAccount(MemberRegisterVO registerVO) {
        String account = registerVO.getAccount();
        String password = registerVO.getPassword();
        String confirmPass = registerVO.getConfirmPass();

        if(StrUtil.isEmpty(account) || StrUtil.isEmpty(password) || StrUtil.isEmpty(confirmPass)) {
            throw new ServiceException(HttpStatus.ERROR,"账号密码不能为空");
        }
        if(!StrUtil.equals(password,confirmPass)) {
            throw new ServiceException(HttpStatus.ERROR,"两次密码不同");
        }
        // 判断用户是否存在
        UmsMember dbMember = memberMapper.selectOne(new LambdaQueryWrapper<UmsMember>().eq(UmsMember::getAccount, account));
        if(ObjectUtil.isNotNull(dbMember)) {
            throw new ServiceException(HttpStatus.ERROR,"该账号已被使用");
        }
        return true;
    }
    public static String generateChineseName() {
        StringBuilder chineseName = new StringBuilder();
        // Unicode范围：4e00-9fa5 是中文字符的范围
        int unicodeStart = 0x4e00;
        int unicodeEnd = 0x9fa5;
        Random random = new Random();
        //参数控制生成名字的长度（2个字/3个字）
        for (int i = 0; i < random.nextInt(2)+2; i++) {
            // 生成随机中文字符
            char randomChar = (char) (unicodeStart + random.nextInt(unicodeEnd - unicodeStart + 1));
            chineseName.append(randomChar);
        }
        return chineseName.toString();
    }

}
